<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <title>advance.html</title>
</head>
<body>
    <h1 id="time"></h1>
    <h1 id="title"></h1>
    <h2 id="message"></h2>
</body>
</html>
<script src="../js/jquery.3.2.1.min.js"></script>
<script src="WebSocket.js?r=5"></script>
<script>
    let cid = getUrlParam('cid');
    if(cid == '') cid = `bobydog_${Math.random()}`;
    //alert(`cid = ${cid}`);
    var wsUrl = `ws://localhost:8080/websocket/${cid}`;
    //调用WebSocket.js中的连接
    createWebSocket(wsUrl);

    // 由各个子系统重写onopen()方法
    function onopenHandler() {
        $('#title').html(`打开了打开了`);
        console.log(`websocket已连接，cid = ${cid}`);
    }
    // 由各个子系统重写onmessage()方法
    function onmessageHandler(data) {
        $('#message').append('<br/>' + getNowTime() + ":" + data);
    }
    // 显示时间
    setInterval(function (){$('#time').html(getNowTime())}, 1000);

    function getNowTime() {
        var nowtime = new Date();
        var year = nowtime.getFullYear();
        var month = nowtime.getMonth() + 1;
        var date = nowtime.getDate();
        return year + "年" + month + "月" + date + "日" + nowtime.toLocaleTimeString();
    }
    function getUrlParam(key) {
        var href = window.location.href;
        //alert(href);
        var url = href.split("?");
        if (url.length <= 1) {
            return "";
        }
        var params = url[1].split("&");
        for (var i = 0; i < params.length; i++) {
            var param = params[i].split("=");
            if (key == param[0]) {
                return param[1];
            }
        }
    }

</script>




























